package day2;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-12-08
 * Time: 13:30
 */
//倒置字符串
//https://www.nowcoder.com/practice/ee5de2e7c45a46a090c1ced2fdc62355?tpId=85&&tqId=29867&rp=
//1&ru=/activity/oj&qru=/ta/2017test/question-ranking
public class InvertString {

    //写一个逆置函数
    public static void niZhi(char[] ch ,int start ,int end){
        while (start < end){
            char tmp = ch[start];
            ch[start] = ch[end];
            ch[end] = tmp;
            start++;
            end--;
        }
    }

    public static void main(String[] args) {
        //将 I like beijing.
        //输入 beijing. like I
        //解体思路先将整体字符倒置 在倒置单个字符
        Scanner scanner = new Scanner(System.in);
        //遇到空格不停下
        String str = scanner.nextLine();

        //toCharArray将字符串 变为字符数组
        char[] ch = str.toCharArray();
        //下标是长度减一
        niZhi(ch,0,str.length()-1);

        //整体逆置完之后 在逆置单个单词
        int i = 0;//定义一个单词的开始
        while (i<str.length()){
            int j = i; //寻找单个词的开始 走到空格结束
            //同时要满足别的条件的话 那么就想到 &&
            while (j<str.length() && ch[j] != ' '){
                j++;
            }

            if (j < str.length()){
                niZhi(ch,i,j-1);
                i = j+1;
            }else {
                //当走到最后一个单词的时候 j是大于str.length()的
                //所以 else 逆置
                niZhi(ch,i,j-1);
                i = j;
                //将j的值 赋值给i i>str.length() 退出循环
            }
        }
        String str2 = new String(ch);
        System.out.println(str2);
    }
}
